﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MODEL;
using System.Data.OleDb;
namespace DAL
{
    public class DalClass
    {
        public int Insert(Class c)
        {
            string sql = "insert into class(pid,classname,type) values(@pid,@name,@type)";
            OleDbParameter[] pms = new OleDbParameter[]
                {
                    new OleDbParameter("@pid",c.Pid),
                    new OleDbParameter("@name",c.ClassName),
                    new OleDbParameter("@type",c.Type)
                };
           return AccessHelp.ExecteNonQuery(sql,pms);
        }
        public List<Class> GetAll()
        {
            string sql = "select id,pid,classname,type from class where del=0 and type=@type";
            OleDbParameter[] pms = new OleDbParameter[]{new OleDbParameter("@type","分类") };
            DataTable dt = AccessHelp.ExecuteReader(sql,pms);
            List<Class> classs = new List<Class>();
            foreach (DataRow row in dt.Rows)
            {
                Class c = new Class();
                c.Id = Convert.ToInt32(row[0].ToString());
                c.Pid = Convert.ToInt32(row[1]);
                c.ClassName = row[2].ToString();
                
                c.Type = row[3].ToString();
                classs.Add(c);
            }
            return classs;
        }


        public bool IsExistByName(string name)
        {
            string sql = "select id from class where classname =@classname ";
            object b = AccessHelp.ExecuteScalar(sql, new OleDbParameter("@classname", name));
            return Convert.ToInt32(b) > 0;
        }
        public bool Delete(int id)
        {
            //string sql = "delete from class where id=@id";
            string sql = "update class set del = 1 where id=@id";
            int result = AccessHelp.ExecteNonQuery(sql,new OleDbParameter("@id", id));
            return result > 0;
        }

        public int Update(Class c)
        {
            string sql = "update class set classname = @classname where id=@id";
            OleDbParameter[] pms = new OleDbParameter[] {
                new OleDbParameter("@classname",c.ClassName),
                new OleDbParameter("@id",c.Id)
            };
            return AccessHelp.ExecteNonQuery(sql,pms);
        }
    }
}
